﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Collections;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore;

namespace STHPMS1.BLL.B_Bus
{
    public class Dp_Proj_RecomBus : BllBase, IDp_Proj_RecomBill
    {
        public void Save<T>(T entity)
            where T : class ,IDp_Proj_Recom
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDp_Proj_Recom entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDp_Proj_Recom
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }



        /// <summary>
        /// 更新综合评定系数
        /// </summary>
        /// <param name="rdwayid"></param>
        public void SetJUDGE_COEF(string anniname, string analy_year)
        {
            Hashtable ht = GetColumHasTable();
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;

            Expression<Func<Dp_Preven_Detail, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year.ToString() && pre.Secon_Measure == 1;

            List<Dp_Preven_Detail> list = this.Scene.Bll.Dp_Preven_Detail.Get<Dp_Preven_Detail>(exp, null).ToList<Dp_Preven_Detail>();
            for (int i = 0; i < list.Count(); i++)
            {
                Dp_Preven_Detail info = list[i];
                double returnvalues = 0;
                //判断自定义权重和分支是否存在（路线编号，复选措施）
                long count = dao.Count<Dp_Sejud_Calcu>(p => p.Rdwayid == info.Rdwayid && p.Secon_Measure == info.Meas_Ini);
                if (count >= 1)
                {
                    returnvalues = this.Scene.Bll.Dp_Sejud_Calcu.GetJUDGE_COEF(info.Rdwayid, info.Meas_Ini);
                }
                else
                {
                    returnvalues = dao.Dp_Proj_Recom.GetJUDGE_COEF(Convert.ToInt32(list[i].Rd_Class), Convert.ToString(ht[list[i].Meas_Ini]));
                }

                dao.Update<Dp_Preven_Detail>(new { Judge_Coef = returnvalues }, p => p.Id == info.Id);
            }
        }


        
        
        /// <summary>
        /// 更新终选措施 设置为1
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <param name="values"></param>
        public void SetPREVEN_MEASURE(string anniname, string analy_year, int values)
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();

            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Update<Dp_Preven_Detail>(new { Preven_Measure = 0 }, p => p.Anni_Name == anniname && p.Analy_Year == analy_year);

            List<string> list = dao.Dp_Preven_Detail.GetMainSegid(anniname, analy_year);
            foreach (string mainsegid in list)
            {
                this.Scene.Dal.Sthpms1.Dp_Proj_Recom.SetPREVEN_MEASURE(anniname, analy_year, mainsegid, values);
            }


            //效益分析表操作
            //dao.Delete<Dp_Costbenefit>(p => p.Rdwayid == rdwayid);

            //Expression<Func<Dp_Preven_Detail, bool>> exp = null;
            //if (rdwayid != null && rdwayid.ToString().Length > 0)
            //    exp = pre => pre.Rdwayid == rdwayid.ToString() && pre.Secon_Measure == 1 && pre.Preven_Measure == 1;

            //List<Dp_Preven_Detail> dp_list = this.Scene.Bll.Dp_Preven_Detail.Get<Dp_Preven_Detail>(exp, null).ToList<Dp_Preven_Detail>();
            //foreach (Dp_Preven_Detail info in dp_list)
            //{
            //    Dp_Costbenefit costinfo = new Dp_Costbenefit();
            //    costinfo.Id = serv.NewGuid();
            //    costinfo.Rdwayid = info.Rdwayid;
            //    costinfo.Mainsegid = info.Mainsegid;
            //    costinfo.Recomm_Prog = info.Meas_Ini;
            //    this.Scene.Bll.Dp_Costbenefit.Save<Dp_Costbenefit>(costinfo);
            //}


        }



        /// <summary>
        /// 计算综合评判系数（权重和分值相乘）
        /// </summary>
        /// <param name="rdclass"></param>
        /// <param name="colums"></param>
        /// <returns></returns>
        public double GetJUDGE_COEF(int rdclass, string colums)
        {
            return this.Scene.Dal.Sthpms1.Dp_Proj_Recom.GetJUDGE_COEF(rdclass, colums);
        }




        public Hashtable GetColumHasTable()
        {
            Hashtable ht = new Hashtable();
            ht.Add("稀浆封层", "SLURRY_SEAL");
            ht.Add("微表处", "MICRO_SUR");
            ht.Add("碎石封层", "CHIP_SEAL");
            ht.Add("复合封层", "COMPO_SEAL");
            ht.Add("薄层罩面", "THIN_OVERLAY");
            ht.Add("雾封层", "FOG_SEAL");
            ht.Add("沥青热再生", "HOT_ASPRECY");
            ht.Add("雷诺锋", "RHINOP");
            ht.Add("纤维沥青碎石封层", "FIBER_SEAL");
            ht.Add("含砂雾封层", "SANDFOG_SEAL");
            ht.Add("沥青还原剂", "PITCH");
            ht.Add("开普封层", "CAPE_SEAL");
            return ht;
        }

    }
}
